package com.xue.utils;

import com.xue.annotation.Column;
import com.xue.annotation.Table;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class JdbcUtil {

    public static List<Object> select(Object condition) {
        List<Object> objects = new ArrayList<>();
        Class<?> aClass = condition.getClass();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet set = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test");
            // 属性名映射字段名
            HashMap<String, String> columnMapper = new HashMap<>();

            // 字段名映射属性名
            HashMap<String, String> fieldMapper = new HashMap<>();

            // 获取属性
            Field[] fields = aClass.getDeclaredFields();

            for (Field field : fields) {
                field.setAccessible(true);
                String fieldName = field.getName();
                if (field.isAnnotationPresent(Column.class)) {
                    Column column = field.getAnnotation(Column.class);
                    // 获取数据字段名称
                    String columnName = column.name();
                    columnMapper.put(fieldName, columnName);
                    fieldMapper.put(columnName, fieldName);
                } else {
                    columnMapper.put(fieldName, fieldName);
                    fieldMapper.put(fieldName, fieldName);
                }
            }

            // 拼接sql语句
            Table table = aClass.getAnnotation(Table.class);

            String sql = "select * from " + table.name();

            StringBuffer where = new StringBuffer(" where 1=1 ");

            for (Field field : fields) {
                Object value = field.get(condition);
                if (null != value) {
                    if (String.class == field.getType()) {
                        where.append(" and " + columnMapper.get(field.getName()) + " = '" + value + "'");
                    } else {
                        where.append(" and " + columnMapper.get(field.getName()) + " = " + value + "");
                    }
                }
            }
            System.out.println(sql + where.toString());
            preparedStatement = connection.prepareStatement(sql + where.toString());

            set = preparedStatement.executeQuery();

            // 获取字段个数
            int columnCount = set.getMetaData().getColumnCount();

            while (set.next()) {
                Object instance = aClass.newInstance();

                for (int i = 1; i < columnCount; i++) {
                    // 获取字段名称
                    String columnName = set.getMetaData().getColumnName(i);

                    Field field = aClass.getDeclaredField(fieldMapper.get(columnName));

                    field.setAccessible(true);
                    field.set(instance, set.getObject(columnName));
                }

                objects.add(instance);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                set.close();
                preparedStatement.close();
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return objects;
    }

}
